package com.qf.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsAdmin;
import com.qf.service.*;
import com.qf.service.impl.DtsGoodsProductServiceImpl;
import com.qf.util.ResponseUtil;
import com.qf.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@CrossOrigin
@Slf4j
@RequestMapping("/admin/admin")
public class AdminController {

    @Autowired
    private DtsAdminService dtsAdminService;
    /**
     * 管理员列表查询+分页
     */
    @RequiresPermissions("admin:admin:list")
    @RequiresPermissionsDesc(menu = {"系统管理","管理员管理"},button = "查询")//按钮
    @GetMapping("/list")
    public Object adminList(
            Integer page, Integer limit,String username,String sort,String order){
        //MybatisPlus: 分页对象
        IPage<DtsAdmin> iPage = dtsAdminService.selectPage(page,limit,username,sort,order);
        Map<String,Object> result=new HashMap<>();
        result.put("total",iPage.getTotal());
        result.put("items",iPage.getRecords());
        return ResponseUtil.ok(result);
    }

    /**
     * 管理员添加
     */
    @RequiresPermissions("admin:admin:create")
    @RequiresPermissionsDesc(menu = {"系统管理","管理员管理"},button = "添加")//按钮
    @PostMapping("/create")
    public Object create(@RequestBody DtsAdmin dtsAdmin, HttpServletRequest request){
        //
        String localAddr = request.getLocalAddr();
        System.out.println(localAddr);
        String remoteAddr = request.getRemoteAddr();
        System.out.println(remoteAddr);// ipv6: 0:0:0:0:.....     ipv4: 127.0.0.1
        dtsAdmin.setLastLoginIp(remoteAddr);
        //密码加密
        Md5Hash md5Hash = new Md5Hash(dtsAdmin.getPassword(),dtsAdmin.getUsername(),1024);
        dtsAdmin.setPassword(md5Hash.toString());
        //添加
        dtsAdmin = dtsAdminService.create(dtsAdmin);
        return ResponseUtil.ok(dtsAdmin);
    }


}
